package com.qf2.exhandlers;

import com.qf2.consts.Code;
import com.qf2.domain.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R ex(Exception e){
        e.printStackTrace();
        //将异常日志记录到日志系统，后续通过邮件通知对应的开发人员

        return R.error("服务器忙，稍后重试");
    }

    @ExceptionHandler(BussinessException.class)
    @ResponseBody
    public R businessEx(BussinessException e){
        e.printStackTrace();

        int code = e.getCode();

        if (code == Code.AGE_LT_0){
            return R.error("年龄不能小于0");
        }else if(code == Code.AGE_GT_120){
            return R.error("年龄不能大于120");
        }else if (code == Code.USERNAME_OR_PWD_ERROR){
            return R.error("用户名或密码错误");
        }

        return R.error(code+"");
    }
}
